home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir26 / epi601_2.zip / FILES06.EXE / ENTFACE.PAS < prev    next >
Pascal/Delphi Source File  |  1994-08-22  |  13KB  |  462 lines

  1. {$R-}    {Range checking off}
  2. {$B+}    {Boolean complete evaluation on}
  3. {$S+}    {Stack checking on}
  4. {$I+}    {I/O checking on}
  5. {$N+}    {No numeric coprocessor}
  6. {$E+}    {Emulation on}
  7. {$V-}    {No string type checking}
  8.  
  9. Unit EntFace;
  10. {This unit provides routines for any Turbo Pascal (version 5+) program
  11.  to install a vector to it's entry point at interrupt 75, and to
  12.  access the data structures FieldList and FieldRecord.  These form
  13.  a linked list of information about all fields in the current
  14.  questionnaire in ENTER.  The routines FindField, GetString, GetNumber,
  15.  PutString, and PutNumber allow access to the FieldList record for
  16.  a named field, and transfer values from (Get) and to (Put) the
  17.  questionnaire field in ENTER.}
  18.  
  19.   Interface
  20.  
  21.     Uses
  22.       Crt,Dos;
  23.  
  24.     const
  25.       EnterError   = 1;
  26.  
  27. {$I ENTTYPES.INC}
  28.  
  29.   {The following routines are available for communication between the ENTER
  30.   program and the TSR program}
  31.  
  32.   Function FindField (Header : FieldPtr; Field : String) : FieldPtr;
  33.   {Returns a pointer to the FieldList record for the field named in Field}
  34.  
  35.   Function InstallInterrupt (IntNo : Integer; ProcPtr : Pointer) : Integer;
  36.   {Installs an interrupt of the IntNo given.  The interrupt vector will
  37.    be ProcPtr, the entry point of your program.
  38.    ProcPtr must point to a function returning an integer, with the format
  39.  
  40.     Function SomeFunc(FilePtr  : FileRecPtr;
  41.                       GlobalVarHeader : VarPtr;
  42.                       Header   : FieldPtr;
  43.                       Current  : FieldPtr;
  44.                       Data     : Integer) : Integer;
  45.   }
  46.  
  47.   Function GetString (Header : FieldPtr; QField : String) : String;
  48.   {Returns a string from a field in the questionnaire}
  49.  
  50.   Function GetNumber (Header : FieldPtr; QField : String) : Float;
  51.   {Returns a number from a field in the questionnaire}
  52.  
  53.   Procedure PutString (Header : FieldPtr; QField : String; S : String);
  54.   {Places S in the named questionnaire field}
  55.  
  56.   Procedure PutNumber (Header : FieldPtr; QField : String; R : Float);
  57.   {Places a number, R, in the named questionnaire field}
  58.  
  59.   function FindVar(FilePtr : FileRecPtr; GlobalVarHeader : VarPtr;
  60.                    VarName : string) : VarPtr;
  61.   {Returns a pointer to the record for the variable named in VarName}
  62.  
  63.   Function GetStringVar (VarP : VarPtr) : String;
  64.   {Returns a string from a variable in the questionnaire}
  65.  
  66.   Function GetNumberVar (VarP : VarPtr) : Float;
  67.   {Returns a number from a variable in the questionnaire}
  68.  
  69.   Procedure PutStringVar (VarP : VarPtr; S : String);
  70.   {Places S in a questionnaire variable}
  71.  
  72.   Procedure PutNumberVar (VarP : VarPtr; R : Float);
  73.   {Places a number, R, in the named questionnaire field}
  74.  
  75.   function RecPos(RecNum : longint; FilePtr : FileRecPtr) : longint;
  76.   {Returns the offset of record number RecNum in the file pointed to by
  77.    FilePtr. NOTE: returns -1 if RecNum < 1 or RecNum > number of records
  78.    in the file. The first record in the file is record #1.}
  79.  
  80.   function RecLength(FilePtr : FileRecPtr) : Integer;
  81.   {Returns the length of a record, as it appears in the disk file, in the
  82.    file pointed to by FilePtr.}
  83.  
  84.   Var
  85.     EnterResult : Integer;
  86.  
  87.   Implementation
  88.  
  89.   Procedure MakeAlfa (S : String80; VAR A : Alfa);
  90.  
  91.     Var
  92.       I : Integer;
  93.  
  94.     Begin
  95.       FillChar(A,SizeOf(A),#32);
  96.       I := Length(S);
  97.       if I > SizeOf(A) then I := SizeOf(A);
  98.       while I > 0 do
  99.       begin
  100.         A[I] := UpCase(S[I]);
  101.         Dec(I);
  102.       end;
  103.     End (*MakeAlfa*);
  104.  
  105.   Procedure MakeString (VAR S : String80; A : Alfa);
  106.  
  107.     Var
  108.       I : Integer;
  109.  
  110.     Begin
  111.       S := '';
  112.       For I := 1 to SizeOf(A) Do
  113.       begin
  114.         If A [I] = #32 then Exit;
  115.         S := S + A[I];
  116.       end;
  117.     End (*MakeString*);
  118.  
  119. Function FindField (Header : FieldPtr; Field : String) : FieldPtr;
  120. (*********************************************************************
  121.  * FindField returns a pointer to the field having name = FieldName. *
  122.  * If no such field exists in the list pointed to by header then     *
  123.  * NIL is returned.                                                  *
  124.  *********************************************************************)
  125.  
  126.   Var
  127.     FPtr  : FieldPtr;
  128.     Found : Boolean;
  129.     Fieldname : Alfa;
  130.     ch : Char;
  131.     I : integer;
  132.  
  133.   Begin
  134.     FPtr := Header;
  135.     Found := False;
  136.     MakeAlfa (Field, Fieldname);
  137.       {Convert fieldname to array and pad with spaces to 10 characters}
  138.     Repeat
  139.       If FPtr ^.Field.Name = FieldName
  140.       Then
  141.         Found := True
  142.       Else
  143.         FPtr := FPtr ^.Next
  144.     Until Found or (FPtr = Header);
  145.     If Found
  146.     Then
  147.       FindField := FPtr
  148.     Else
  149.       begin
  150.         FindField := NIL;
  151.         GotoXY (1,25);
  152.         Write
  153.  ('Field ',Field,' not found. Please check TSR program and questionnaire.');
  154.         ch := readkey;
  155.       end;
  156.   End (*FindField*);
  157.  
  158. Function InstallInterrupt (IntNo : Integer; ProcPtr : Pointer) : Integer;
  159.  
  160.     Type
  161.       InterruptRecPtr  = ^ InterruptRec;
  162.       InterruptRec     = Packed Record
  163.                            JmpInst      : Byte;
  164.                            OldInt       : Pointer;
  165.                            IDString     : Array [1 .. 5] of Char;
  166.                            OldDSValue   : Word;
  167.                            EnterRoutine : Pointer
  168.                          End (*InterruptRec*);
  169.  
  170.     Const
  171.       InterruptRoutine : InterruptRec =
  172.                            (JmpInst:      $EA;
  173.                             OldInt:       NIL;
  174.                             IDString:     'ENTER';
  175.                             OldDSValue:   0;
  176.                             EnterRoutine: NIL);
  177.  
  178.     Var
  179.       Dummy : Pointer;
  180.  
  181.     Function GetDSValue : Word;
  182.  
  183.       Inline ($8C/$D8)   {MOV   AX,DS};
  184.  
  185.     Begin
  186.       GetIntVec (IntNo, Dummy);
  187.       If InterruptRecPtr (Dummy) ^.IDString = 'ENTER'
  188.       Then
  189.         InstallInterrupt := 1
  190.       Else
  191.         Begin
  192.           InterruptRoutine.OldInt := Dummy;
  193.           InterruptRoutine.EnterRoutine := ProcPtr;
  194.           InterruptRoutine.OldDSValue := GetDSValue;
  195.           SetIntVec (IntNo, @InterruptRoutine);
  196.           InstallInterrupt := 0
  197.         End (*Else*)
  198.     End (*InstallInterrupt*);
  199.  
  200. Function GetString (Header : FieldPtr; QField : String) : String;
  201.   {Returns a string from a field in the questionnaire}
  202.  Var FPtr : FieldPtr;
  203.  
  204.     Begin
  205.       FPtr := FindField (Header, QField);
  206.       With FPtr ^ Do
  207.         If Missing
  208.         Then
  209.           GetString := ''
  210.         Else
  211.           GetString := FieldEntry
  212.     End (*GetString*);
  213.  
  214. Function GetNumber (Header : FieldPtr; QField : String) : Float;
  215.   {Returns a number from a field in the questionnaire}
  216.  
  217.  Var FPtr : FieldPtr;
  218.  
  219.     Begin
  220.       FPtr := FindField (Header, QField);
  221.       GetNumber := 0;
  222.       With FPtr ^ Do
  223.         If Not Missing
  224.         Then
  225.           Case Field.EntryKind of
  226.             Numeric:
  227.               GetNumber := FieldInt;
  228.             RealNum:
  229.               GetNumber := FieldReal;
  230.             Else
  231.               EnterResult := EnterError
  232.           End (*Case*)
  233.     End (*GetNumber*);
  234.  
  235. Function TruncDecimals (R : Float; NumDecimals : Integer) : Float;
  236.  
  237.     Var
  238.       Temp : Float;
  239.  
  240.     Begin
  241.       Temp := 1;
  242.       While (NumDecimals > 0) Do
  243.         Begin
  244.           Temp := Temp * 10;
  245.           Dec (NumDecimals)
  246.         End (*While*);
  247.       R := Round (R * Temp);
  248.       TruncDecimals := R / Temp
  249.     End (*TruncDecimals*);
  250.  
  251.  
  252. Procedure PutString (Header : FieldPtr; QField : String; S : String);
  253.   {Places S in the named questionnaire field}
  254.  
  255.     Var
  256.       I, J : Integer;
  257.       R    : Float;
  258.       FPtr : FieldPtr;
  259.  
  260.     Begin
  261.       FPtr := FindField (Header, QField);
  262.       With FPtr ^ Do
  263.         If S = ''
  264.         Then
  265.           Begin
  266.             Missing := True;
  267.             FieldEntry := ''
  268.           End (*If*)
  269.         Else
  270.           Begin
  271.             J := 0;
  272.             Case Field.EntryKind of
  273.               Numeric:
  274.                 Begin
  275.                   Val (S, I, J);
  276.                   If J = 0
  277.                   Then
  278.                     PutNumber (Header, QField, I)
  279.                 End (*Numeric*);
  280.               RealNum:
  281.                 Begin
  282.                   Val (S, R, J);
  283.                   If J = 0
  284.                   Then
  285.                     PutNumber (Header, QField, R)
  286.                 End (*RealNum*);
  287.               Else
  288.                 Begin
  289.                   If Length (S) > Field.EntryLen
  290.                   Then
  291.                     S [0] := Chr (Field.EntryLen);
  292.                   FieldEntry := S;
  293.                   Missing := False;
  294.                 End (*Else*)
  295.             End (*Case*);
  296.             If J <> 0
  297.             Then
  298.               EnterResult := EnterError
  299.           End (*Else*)
  300.     End (*PutString*);
  301.  
  302.   Function MakeStringFromReal (R : Float; Width : Integer) : String;
  303.  
  304.     Var
  305.       S : String;
  306.       SLen : Byte Absolute S;
  307.  
  308.     Begin
  309.       Str (R: Width*2: Width, S);
  310.       While (S [SLen] = '0') Do
  311.         Dec (SLen);
  312.       If S [SLen] = '.'
  313.       Then
  314.         Dec (SLen);
  315.       While (S [1] = ' ') And (SLen > 0) Do
  316.         Delete (S, 1, 1);
  317.       MakeStringFromReal := S
  318.     End (*MakeStringFromReal*);
  319.  
  320.  
  321. Procedure PutNumber (Header : FieldPtr; QField : String; R : Float);
  322.   {Places a number, R, in the named questionnaire field}
  323.  
  324.  
  325.     Var
  326.       I : Integer;
  327.    FPtr : FieldPtr;
  328.  
  329.     Begin
  330.       FPtr := FindField (Header, QField);
  331.       With FPtr ^ Do
  332.         Begin
  333.           Missing := False;
  334.           Case Field.EntryKind of
  335.             Numeric:
  336.               Begin
  337.                 I := Round (R);
  338.                 Str (I: Field.EntryLen, FieldEntry);
  339.                 FieldInt := I;
  340.               End (*Numeric*);
  341.             RealNum:
  342.               begin
  343.                 If Field.Decimals > 0 Then
  344.                   Begin
  345.                     R := TruncDecimals (R, Field.Decimals);
  346.                     Str (R: Field.EntryLen: Field.Decimals, FieldEntry)
  347.                   End (*If*)
  348.                 Else
  349.                   FieldEntry := MakeStringFromReal (R, Field.EntryLen);
  350.                 FieldReal := R;
  351.               end;
  352.             Else
  353.               FieldEntry := MakeStringFromReal (R, Field.EntryLen)
  354.           End (*Case*)
  355.         End (*With*)
  356.     End (*PutNumber*);
  357.  
  358. Function NumberOfRecords (FilePtr : FileRecPtr) : LongInt;
  359. Begin
  360.   With FilePtr ^ Do
  361.     NumberOfRecords := ((FileSize (F) - DataOffset) DIV RecSize);
  362. End (*NumberOfRecords*);
  363.  
  364. function VarSearch(VPtr : VarPtr; Vname : String20) : VarPtr;
  365. var
  366.   vp : VarPtr;
  367. begin {VarSearch}
  368.   if VPtr = nil then VarSearch := nil
  369.   else if VPtr^.Name = Vname then VarSearch := VPtr
  370.   else begin
  371.     vp := VarSearch(VPtr^.Left,Vname);
  372.     if vp = nil then vp := VarSearch(VPtr^.Right,Vname);
  373.     VarSearch := vp;
  374.   end;
  375. end; {VarSearch}
  376.  
  377. function FindVar(FilePtr : FileRecPtr; GlobalVarHeader : VarPtr;
  378.                  VarName : string) : VarPtr;
  379. {Returns a pointer to the record for the variable named in VarName}
  380.   Var
  381.     VPtr  : VarPtr;
  382.     Vname : String20;
  383.     ch : Char;
  384.     I : integer;
  385.  
  386.   Begin {FindVar}
  387.     VPtr := nil;
  388.     Vname := VarName;
  389.     for I := 1 to Length(Vname) do Vname[I] := UpCase(Vname[I]);
  390.     if Vname <> '' then
  391.     begin
  392.       VPtr := VarSearch(FilePtr^.LocalVarHeader,Vname);
  393.       if VPtr = nil then
  394.           VPtr := VarSearch(GlobalVarHeader,Vname);
  395.     end;
  396.     if VPtr = nil then
  397.     begin
  398.       GotoXY (1,25);
  399.       Write
  400.  ('Variable ',VarName,' not found. Please check TSR program and questionnaire.');
  401.       ch := readkey;
  402.     end;
  403.     FindVar := VPtr;
  404.   End; {FindVar}
  405.  
  406. function GetStringVar (VarP : VarPtr) : string;
  407. {Returns a string from a variable in the questionnaire}
  408. begin
  409.   if VarP = nil then GetStringVar := ''
  410.   else GetStringVar := VarP^.VAlfa;
  411. end; {GetStringVar}
  412.  
  413. function GetNumberVar (VarP : VarPtr) : Float;
  414. {Returns a number from a variable in the questionnaire}
  415. begin
  416.   if VarP = nil then GetNumberVar := 0.0
  417.   else GetNumberVar := VarP^.VFloat;
  418. end; {GetNumberVar}
  419.  
  420. procedure PutStringVar (VarP : VarPtr; S : string);
  421. {Places S in a questionnaire variable}
  422. begin
  423.   if VarP <> nil then
  424.     with VarP^ do
  425.   begin
  426.     VAlfa := S;
  427.     Missing := S = '';
  428.   end;
  429. end; {PutStringVar}
  430.  
  431. procedure PutNumberVar (VarP : VarPtr; R : Float);
  432. {Places a number, R, in the named questionnaire field}
  433. begin
  434.   if VarP <> nil then
  435.     with VarP^ do
  436.   begin
  437.     VFloat := R;
  438.     Missing := False;
  439.   end;
  440. end; {PutNumberVar}
  441.  
  442. function RecPos(RecNum : longint; FilePtr : FileRecPtr) : longint;
  443. {Returns the offset of record number RecNum in the file pointed to by
  444.  FilePtr. NOTE: returns -1 if RecNum < 1 or RecNum > number of records
  445.  in the file.}
  446. begin {RecPos}
  447.   if FilePtr = nil then RecPos := 0
  448.   else with FilePtr^ do
  449.     if (RecNum < 1) or (RecNum > NumberOfRecords(FilePtr)) then RecPos := -1
  450.     else RecPos := DataOffset + (RecNum-1)*RecSize;
  451. end; {RecPos}
  452.  
  453. function RecLength(FilePtr : FileRecPtr) : Integer;
  454. {Returns the length of a record, as it appears in the disk file, in the
  455.  file pointed to by FilePtr.}
  456. begin
  457.   if FilePtr = nil then RecLength := 0
  458.   else RecLength := FilePtr^.RecSize;
  459. end; {RecLength}
  460.  
  461. End.  (*ENTFACE.PAS*)
  462.